package com.qidian.md.controller;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Calendar;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/26 19:32
 * @Description
 */
@RestController
@RequestMapping("/gen")
public class AutomaticallyGenerateCode {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping
    public MESResult gen() {
        String key = "XXXID";
        StringBuilder id = new StringBuilder();
        id.append("Client"); // Fixed part, can be retrieved from the database
        Calendar calendar = Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR) + "" + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DATE));
        Long idTemp = stringRedisTemplate.opsForValue().increment(key);
        String idStr = idTemp.toString();
        if (idStr.length() < 5) {
            for (int i = 0; i < 5 - idStr.length(); i++) {
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }
}